package hsl.study.controller;


import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: hsl
 * @CreateDate: 2025/7/28 17:51
 * @Version: 1.0
 */

@RestController
@RequestMapping("/OpenAI")
public class OpenAIController {
    // v1
    @Resource
    private ChatModel chatModel;

    // v2
    private ChatClient chatClient;
    public OpenAIController(ChatModel chatModel){
        this.chatClient = ChatClient.builder(chatModel).build();
    }

    //v3
    @Resource
    private OpenAiChatModel openAiChatModel;

    @GetMapping("/chat1")
    public String chat(@RequestParam(name = "question",defaultValue = "你是谁") String question)
    {
        String result = chatModel.call(question);
        System.out.println("chat result:" + result);
        return result;
    }

    @RequestMapping("/chat2")
    public String chat2(@RequestParam(name = "question",defaultValue = "你是谁") String question)
    {
        ChatResponse chatResponse = chatClient.prompt(question).call().chatResponse();
        String result = chatResponse.getResult().getOutput().getText();
        System.out.println("chat2 result:" + result);
        return result;
    }

    @GetMapping("/chat3")
    public String chat3(@RequestParam(name = "question",defaultValue = "你是谁") String question)
    {
        String result = openAiChatModel.call(question);
        System.out.println("chat3 result:" + result);
        return result;
    }
}
